import 'package:animations/animations.dart';
import 'package:craze/pages/index.dart';
import 'package:craze/pages/yi/question_detail_view.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:craze/pages/san/ai_vision/ai_result_view.dart';
import 'dart:io';

class AppRouter {
  static const String home = '/';
  static const String start = '/start';
  static const String aiVision = '/ai-vision';
  static const String choose = '/choose';
  static const String login = '/login';
  static const String register = '/register';
  static const String otherProfile = '/other-profile';
  static const String follow = '/follow';
  static const String fans = '/fans';
  static const String collect = '/collect';
  static const String message = '/message';
  static const String chat = '/chat/:id';
  static const String groupChat = '/group-chat/:id';
  static const String edit = '/edit';
  static const String setup = '/setup';
  static const String knowledgeDetailOne = '/knowledge-detail-one';
  static const String knowledgeDetailTwo = '/knowledge-detail-two';
  static const String knowledgeDetailThree = '/knowledge-detail-three';
  static const String test = '/test';
  static const String answer = '/answer';
  static const String video = '/video';
  static const String createTeam = '/create-team';
  static const String uploadShare = '/upload-share';
  static const String protection = '/protection';
  static const String scan = '/scan';
  static const String team = '/team';
  static const String shareDetail = '/share-detail/:id';
  static const String aiResult = '/ai-result';

  static CustomTransitionPage<void> _buildTransitionPage({
    required BuildContext context,
    required GoRouterState state,
    required Widget child,
  }) {
    return CustomTransitionPage<void>(
      key: state.pageKey,
      child: child,
      transitionsBuilder: (context, animation, secondaryAnimation, child) {
        return FadeScaleTransition(animation: animation, child: child);
      },
    );
  }

  static final GoRouter router = GoRouter(
    initialLocation: start,
    debugLogDiagnostics: true, // 日志
    routes: [
      GoRoute(
        path: home,
        name: 'home',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const MainView(),
            ),
      ),
      GoRoute(
        path: start,
        name: 'start',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const StartView(),
            ),
      ),
      // GoRoute(
      //   path: aiVision,
      //   name: 'aiVision',
      //   pageBuilder:
      //       (context, state) => _buildTransitionPage(
      //         context: context,
      //         state: state,
      //         child: const AiVisionView(),
      //       ),
      // ),
      GoRoute(
        path: choose,
        name: 'choose',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const ChooseView(),
            ),
      ),
      GoRoute(
        path: login,
        name: 'login',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const LoginView(),
            ),
      ),
      GoRoute(
        path: register,
        name: 'register',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const RegisterView(),
            ),
      ),
      GoRoute(
        path: otherProfile,
        name: 'otherProfile',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: OtherProfileView(
                userId:
                    state.extra != null
                        ? (state.extra as Map<String, dynamic>)['userId']
                            as String?
                        : null,
              ),
            ),
      ),
      GoRoute(
        path: follow,
        name: 'follow',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const FollowView(),
            ),
      ),
      GoRoute(
        path: fans,
        name: 'fans',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const FanView(),
            ),
      ),
      GoRoute(
        path: message,
        name: 'message',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const ChatListView(),
            ),
      ),
      GoRoute(
        path: collect,
        name: 'collect',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const CollectView(),
            ),
      ),
      GoRoute(
        path: chat,
        name: 'chat',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: ChatView(
                chatId: state.pathParameters['id'] ?? '',
                isGroupChat: false,
              ),
            ),
      ),
      GoRoute(
        path: groupChat,
        name: 'groupChat',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: ChatView(
                chatId: state.pathParameters['id'] ?? '',
                isGroupChat: true,
              ),
            ),
      ),
      GoRoute(
        path: edit,
        name: 'edit',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const EditView(),
            ),
      ),
      GoRoute(
        path: setup,
        name: 'setup',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const SetupView(),
            ),
      ),
      GoRoute(
        path: knowledgeDetailOne,
        name: 'knowledgeDetailOne',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const KnowledgeDetailOneView(),
            ),
      ),
      GoRoute(
        path: knowledgeDetailTwo,
        name: 'knowledgeDetailTwo',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const KnowledgeDetailTwoView(),
            ),
      ),
      GoRoute(
        path: knowledgeDetailThree,
        name: 'knowledgeDetailThree',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const KnowledgeDetailThreeView(),
            ),
      ),
      // 问题详情页面路由
      GoRoute(
        path: answer,
        name: 'answer',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const QuestionDetailView(),
            ),
      ),
      GoRoute(
        path: video,
        name: 'video',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const VideoView(),
            ),
      ),
      GoRoute(
        path: createTeam,
        name: 'createTeam',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const CreateTeamView(),
            ),
      ),
      GoRoute(
        path: uploadShare,
        name: 'uploadShare',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const UploadShareView(),
            ),
      ),
      GoRoute(
        path: protection,
        name: 'protection',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: ProtectionView(
                activityId: state.uri.queryParameters['activityId'],
              ),
            ),
      ),
      GoRoute(
        path: scan,
        name: 'scan',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const ScanView(),
            ),
      ),
      GoRoute(
        path: team,
        name: 'team',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: const TeamView(),
            ),
      ),
      GoRoute(
        path: shareDetail,
        name: 'shareDetail',
        pageBuilder:
            (context, state) => _buildTransitionPage(
              context: context,
              state: state,
              child: ShareDetailView(shareId: state.pathParameters['id'] ?? ''),
            ),
      ),

      // AI 结果页面路由
      GoRoute(
        path: aiResult,
        name: 'aiResult',
        pageBuilder: (context, state) {
          final Map<String, dynamic> extra =
              state.extra as Map<String, dynamic>;
          return _buildTransitionPage(
            context: context,
            state: state,
            child: AIResultView(
              imageFile: extra['imageFile'] as File?,
              question: extra['question'] as String,
            ),
          );
        },
      ),
    ],
    errorBuilder: (context, state) => const NotFoundPage(),
  );
}
